package com.datoucai.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigChangeEvent;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * nacos 配置监听 
 */
@Slf4j
@Component(value = "nacosConfigListener")
public class NacosConfigListener extends AbstractConfigChangeListener implements InitializingBean {

    @Resource
    private NacosConfigManager nacosConfigManager;

    @Override
    public void afterPropertiesSet() throws Exception {
        nacosConfigManager.getConfigService().addListener("dtc-comment-all-env.yaml", "DEFAULT_GROUP", this);
    }

    @Override
    public void receiveConfigChange(ConfigChangeEvent event) {

        for (ConfigChangeItem changeItem : event.getChangeItems()) {
            String key = changeItem.getKey();
            String oldValue= changeItem.getOldValue();
            String newValue= changeItem.getNewValue();
            System.out.println("变化key: " + key);
            System.out.println("旧值: " + oldValue);
            System.out.println("新值: " + newValue);
        }
    }
}
